package fy.util;

import java.lang.reflect.Field;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONObject;

public class MyJSONConvert {

	public static String toJSONStr(Object o) {
		if (o != null && !(o instanceof String)) {
			if (o instanceof List) {
				return toJSONArray((List) o).toString();
			} else {
				return toJSONObject(o).toString();
			}
		} else {
			return "{}";
		}
	};

	private static JSONObject toJSONObject(Object o) {

		JSONObject jo = new JSONObject();
		try {
			if (o instanceof Map) {
				Map temp = (Map) o;
				for (Iterator iter = temp.keySet().iterator(); iter.hasNext();) {
					Object key = iter.next();
					Object value = temp.get(key);
					if (value == null || value instanceof String || value instanceof Comparable) {
						jo.put(key.toString(), value);
					} else {
						if (value instanceof List)
							jo.put(key.toString(), toJSONArray((List) value));
						else if (value instanceof Object[])
							jo.put(key.toString(),
									toJSONArray((Object[]) value));
						else
							jo.put(key.toString(), toJSONObject(value));
					}
				}
			} else {
				Field[] fields = o.getClass().getDeclaredFields();

				if (fields != null) {
					for (int i = 0, length = fields.length; i < length; i++) {
						fields[i].setAccessible(true);
						Object value = fields[i].get(o);
						fields[i].setAccessible(false);
						String name = fields[i].getName();
						if (value == null || value instanceof String  || value instanceof Comparable) {
							jo.put(name, value);
						} else if (value instanceof List) {
							jo.put(name, toJSONArray((List) value));
						} else if (value instanceof Map) {
							jo.put(name, toJSONObject(value));
						} else if (value instanceof Object[]) {
							jo.put(name, toJSONArray((Object[]) value));
						} else {
							jo.put(name, toJSONObject(value));
						}
					}
				}

			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return jo;
	}

	private static JSONArray toJSONArray(List o) {
		JSONArray ja = new JSONArray();
		for (int i = 0, length = o.size(); i < length; i++) {
			if (o.get(i) instanceof List) {
				ja.put(toJSONArray((List) o.get(i)));
			} else if (o.get(i) instanceof Object[]) {
				ja.put(toJSONArray((Object[]) o.get(i)));
			} else {
				if (o.get(i) == null || o.get(i) instanceof String  || o.get(i) instanceof Comparable) {
					ja.put(o.get(i));
				} else {
					ja.put(toJSONObject(o.get(i)));
				}
			}
		}
		return ja;
	}

	private static JSONArray toJSONArray(Object[] o) {
		JSONArray ja = new JSONArray();
		for (int i = 0, length = o.length; i < length; i++) {
			if (o[i] instanceof List) {
				ja.put(toJSONArray((List) o[i]));
			} else if (o[i] instanceof Object[]) {
				ja.put(toJSONArray((Object[]) o[i]));
			} else {
				if (o[i] == null || o[i] instanceof String || o[i] instanceof Comparable) {
					ja.put(o[i]);
				} else {
					ja.put(toJSONObject(o[i]));
				}
			}
		}
		return ja;
	}
}
